package com.paul.sys.controller.back;

import com.paul.sys.model.BikeParkInfo;
import com.paul.sys.service.BikeParkInfoService;
import com.paul.sys.util.ParamUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import net.sf.json.JSONObject;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 羽 on 2015/11/9.
 */
@Controller
@RequestMapping("/back/bike/")
public class BikeParkController {
    private static Logger logger=Logger.getLogger(BikeParkController.class);

    @Autowired
    private BikeParkInfoService bikeParkInfoService;

    @RequestMapping("ismenu/bike")
    public ModelAndView role() throws Exception {

        ModelAndView view = new ModelAndView("back/bikePark/bikePark-list");
        Integer total = bikeParkInfoService.countAll(new HashMap());
        view.addObject("total", total);
        return view;
    }

    @RequestMapping("edit")
    public ModelAndView modify(HttpServletRequest request) throws Exception {
        ModelAndView view = new ModelAndView("back/bikePark/bikePark-edit");
        Integer unid = Integer.parseInt(request.getParameter("unid"));
        BikeParkInfo entity  = bikeParkInfoService.selectByPrimaryKey(unid);
        view.addObject("bikepark", entity);
        return view;
    }

    @ResponseBody
    @RequestMapping("getCount")
    public Integer countSelect(HttpServletRequest request) throws Exception {
        Integer totalselect = 0;
        try {
            ParamUtil util = new ParamUtil(request);
            Map<String, String> param = util.getParamMap();
            totalselect = bikeParkInfoService.countAll(param);
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }

        return totalselect;

    }


    @ResponseBody
    @RequestMapping("updateBikePark")
    public Map<String, Object> setBikePark(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            ParamUtil util = new ParamUtil(request);
            Map<String,String> param = util.getParamMap();
            JSONObject jsonObject = JSONObject.fromObject(param);
            BikeParkInfo bikeparkinfo = (BikeParkInfo) JSONObject.toBean(jsonObject,BikeParkInfo.class);
            bikeParkInfoService.updateByBike(bikeparkinfo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("bikePark-Map")
    public ModelAndView localMap(String lng,String lat) throws Exception {
        ModelAndView view = new ModelAndView("back/bikePark/bikePark-Map");
        view.addObject("lng",lng);
        view.addObject("lat",lat);
        return view;
    }

    @ResponseBody
    @RequestMapping("list")
    public Map<String, Object> listPtstaff(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>();

        try {
            ParamUtil util = new ParamUtil(request);
            Map<String, String> param = util.getParamMap();
            List<BikeParkInfo> data = bikeParkInfoService.listAll(param);
            result.put("data", data);
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }

        return result;
    }

}
